/**
 * HTTP请求工具函数
 * 使用现有的alova实例，避免重复创建
 */
import { alovaInstance } from '@/api'

// 封装HTTP方法
export function httpGet<T = any>(url: string, params?: any) {
  return alovaInstance.Get<T>(url, { params })
}

export function httpPost<T = any>(url: string, data?: any) {
  return alovaInstance.Post<T>(url, data)
}

export function httpPut<T = any>(url: string, data?: any) {
  return alovaInstance.Put<T>(url, data)
}

export function httpDelete<T = any>(url: string, params?: any) {
  return alovaInstance.Delete<T>(url, { params })
}

export function getApiUrl(url: string) {
  const baseUrl = import.meta.env.VITE_SERVER_BASEURL || ''
  const apiUrl = import.meta.env.VITE_API_URL || ''
  if (!url.startsWith('/'))
    url = `/${url}`
  return baseUrl + apiUrl + url
}
